import zipfile, os  

class vfs_tree_node(object):

    def __init__(self, name, parent=None, node_type="file"):
        self.name = name
        self.parent = parent
        self.node_type = node_type
        self.children = []
        if parent != None :
            parent.children.append(self)

    def getChild(self, name):
        for child in self.children :
            if child.name == name :
                return child
        return None

def print_node(node, level = 0):
    print level * 4 * " " + node.name
    for item in node.children :
        print_node(item , level+1)

def add_tree_entry(root, line):
    sections = line.split("/")
    parentNode = root

    for index, item in enumerate(sections):
        if item == "" :
            continue
        sameChild = parentNode.getChild(item)
        if sameChild == None :
            node_type = "dir"
            if index == len(sections)-1 :
                node_type="file"
            node = vfs_tree_node(item, parentNode,node_type)
            parentNode = node
        else :
            parentNode = sameChild

def build_zip_tree():
    zipFile = zipfile.ZipFile(os.path.join(os.getcwd(), 'commons-io-1.4.jar'))  
    root = vfs_tree_node("root",node_type="dir")
    for name in zipFile.namelist() :
        add_tree_entry(root,name)
    return root

if __name__ == "__main__" :
    root = build_zip_tree()
    print_node(root)

